package com.mindskip.xzs.security;

import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * 自定义一个RememberMeServices
 */
public class RestTokenBasedRememberMeServices extends TokenBasedRememberMeServices {

    public RestTokenBasedRememberMeServices(String key, UserDetailsService userDetailsService) {
        super(key, userDetailsService);
    }

    // rememberMeRequested的作用是判断登录请求是否有"记住我"的功能
    // 父类的rememberMeRequested方法是从请求参数中判断是否有"remember-me"属性，这里是前后端分离，为post请求，不能从请求参数中获取，所以得重新实现该方法
    // 判断request域中是否有"remember-me"的属性
    @Override
    protected boolean rememberMeRequested(HttpServletRequest request, String parameter) { // parameter的值默认为"remember-me"
        // 从request域中获取"remember-me"的属性
        boolean paramValue = (boolean) request.getAttribute(parameter);

        if (paramValue) {
            return true;
        }

        if (logger.isDebugEnabled()) {
            logger.debug("Did not send remember-me cookie (principal did not set parameter '"
                    + parameter + "')");
        }

        return false;
    }

}
